package e.odbo.data.generate.code.model;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 表结构信息
 *
 * @author zhanglikun
 * @mail likun_zhang@yeah.net
 * @date 2013-6-6
 */
public class TableMeta {
    //private String schemaName ;
    private String basePackageName;

    private String tableName;
    private String comment;
    private ArrayList<ColumnMeta> columns = new ArrayList<>();

    private ArrayList<String> importInterfaces = new ArrayList<>();

    private ArrayList<String> importBeanClass = new ArrayList<>();

    private boolean modifiedLogAble = false;

    private boolean autoGeneratorID = false;

    private boolean createDateAble = false;

    int idIndex = -1;

    public boolean isModifiedLogAble() {
        return modifiedLogAble;
    }

    public void setModifiedLogAble(boolean modifiedLogAble) {
        this.modifiedLogAble = modifiedLogAble;
    }

    public boolean isAutoGeneratorID() {
        return autoGeneratorID;
    }

    public void setAutoGeneratorID(boolean autoGeneratorID) {
        this.autoGeneratorID = autoGeneratorID;
    }

    public boolean isCreateDateAble() {
        return createDateAble;
    }

    public void setCreateDateAble(boolean createDateAble) {
        this.createDateAble = createDateAble;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public List<ColumnMeta> getColumns() {
        return columns;
    }

    public void setColumns(ArrayList<ColumnMeta> columns) {
        this.columns = columns;
    }


    public String getClassName() {
        return getClassName(tableName);
    }

    public static String getClassName(String tableName) {
        if (tableName == null) return "";
        StringBuffer className = new StringBuffer();
        String[] names = StringUtils.split(tableName.toLowerCase(), "_");
        for (int i = 0, len = names.length; i < len; i++) {
            className.append(names[i].substring(0, 1).toUpperCase() + names[i].substring(1));
        }
        return className.toString();
    }

    public String getBasePackageName() {
        return basePackageName;
    }

    public void setBasePackageName(String basePackageName) {
        this.basePackageName = basePackageName;
    }

    /**
     * public String getSchemaName() {
     * return schemaName;
     * }
     * public void setSchemaName(String schemaName) {
     * this.schemaName = schemaName;
     * }
     **/

    public ArrayList<String> getImportInterfaces() {
        return importInterfaces;
    }

    public void setImportInterfaces(ArrayList<String> importInterfaces) {
        this.importInterfaces = importInterfaces;
    }

    public void addImportInterface(String classWithPackage) {
        importInterfaces.add(classWithPackage);
    }


    public ColumnMeta getColumnByName(String columnName) {
        for (ColumnMeta columnMeta : columns)
            if (columnMeta.getColumnName().equalsIgnoreCase(columnName))
                return columnMeta;
        return null;
    }

    public void setIdIndex(int columnIndex) {
        idIndex = columnIndex;
    }

    public String getImplementStr() {
        String implementsStr = " implements ";
        if (importInterfaces.size() == 0)
            return "";
        boolean addSp = false;
        if (this.autoGeneratorID) {
            implementsStr += "I_AutoGeneratorStringKey";
            addSp = true;
        }
        if (this.createDateAble) {
            implementsStr += addSp ? " ,CreateDateAble " : " CreateDateAble ";
            addSp = true;
        }
        if (this.modifiedLogAble) {
            implementsStr += addSp ? " ,ModifiedLogAble " : " ModifiedLogAble ";
        }
        return implementsStr;
    }

    public void addImportBeanClass(String classWithPackage) {
        importBeanClass.add(classWithPackage);
    }


    public ArrayList<String> getImportBeanClass() {
        return importBeanClass;
    }

    public void setImportBeanClass(ArrayList<String> importBeanClass) {
        this.importBeanClass = importBeanClass;
    }

    public int getIdIndex() {
        return idIndex;
    }

    public void addColumn(ColumnMeta columnMeta) {
        this.columns.add(columnMeta);
    }
}